local MeleeHead = MClass("MeleeHead")
local TAG_CD = 101

function MeleeHead:ctor()
    --self.root 默认为根节点
    --设置事件
    self.panel = self:findChild("Panel")
    self.teamBg = self.panel:findChild("Panel_bg/bg_team")
    self.enemyBg = self.panel:findChild("Panel_bg/bg_foe")
    self.headIcon = self.panel:findChild("Panel_head/icon_img")
    self.bg = self.panel:findChild("Panel_bg")
    self.deadSign = self.panel:findChild("Panel_sign")
    self.arrow = self.panel:findChild("arrow")
    --cd

     --cd
    self.mCdNode = self.panel:findChild("Panel_cd")
    assert(self.mCdNode)
    self.mCdMask = self.mCdNode:findChild("img_cd")
    assert(self.mCdMask)
    self.mCdMask:setVisible(false)
    self.cdSprite = self.mCdNode:getChildByTag(TAG_CD)
    if not self.cdSprite then
        self.cdSprite = cc.ProgressTimer:create( self.mCdMask )
        self.cdSprite:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
        self.cdSprite:setMidpoint(cc.p( 0.5, 0.5 ))
        self.cdSprite:setPosition(self.mCdMask:getPosition())
        self.cdSprite:setTag(TAG_CD)
        self.cdSprite:setScaleX(-0.5)
        self.cdSprite:setScaleY(0.5)
        self.mCdNode:addChild(self.cdSprite, -1)
    end

    self.mCdNum = self.mCdNode:findChild("time")
    assert(self.mCdNum)

    self:init()
end

function MeleeHead:init()
	self.arrow:setVisible(false)
end

function MeleeHead:setEnemy(bEnemy)
	if bEnemy then
		self.teamBg:setVisible(false)
		self.enemyBg:setVisible(true)
	else
		self.enemyBg:setVisible(false)
		self.teamBg:setVisible(true)
	end
end

function MeleeHead:setIcon(iconFile)
	if self.headIcon and iconFile then
		self.headIcon:loadTexture(iconFile)
	end
end

function MeleeHead:setDead(bDead)
	self.deadSign:setVisible(bDead)
	if not self.bSelf then
		self.bg:setVisible(not bDead)
	end
end

function MeleeHead:setBeSelf(bSelf)
	self.bSelf = bSelf
	self.arrow:setVisible(bSelf)
end

function MeleeHead:setCD(time,needTime)
    if not cc.isDead(self.mCdNode) then
        if time<=0 then
            self.mCdNode:setVisible(false)
        else
            self.mCdNode:setVisible(true)
            self.mCdNum:setString(string.format("%.1f", time/1000))
            local percent = time/needTime
            self.cdSprite:setPercentage(100*percent)
        end
    end
end

function MeleeHead:setShouldRemove(bFlag)
    self.shouldRemove = bFlag
end

function MeleeHead:getShouldRemove()
    return self.shouldRemove
end

return MeleeHead